package com.example.myspringboot.controller;

import com.example.myspringboot.bean.User;
import com.example.myspringboot.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestBody User user) {  //由于axios.post传的是json数据，必须要这样!
        int i = userMapper.login(user);        //以前是用User user接收，不过万一数据库有两条信息一样就无法登录，返回数据的count就行了
        if (i > 0) {
            return "登录成功";
        } else {
            return "登录失败";
        }
    }

    @RequestMapping("/checkusername")
    @ResponseBody
    public String checkusername(String username) {
        int i = userMapper.checkusername(username);
        if (i > 0) {
            return "用户存在";
        } else {
            return "用户不存在";
        }
    }

    @RequestMapping("/register")
    @ResponseBody
    public String register(@RequestBody User user) {
        int i = userMapper.register(user);
        if (i == 1) {
            return "注册成功";
        } else {
            return "注册失败";
        }
    }
}
